home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7064 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  66 lines

  1. Path: news.acns.nwu.edu!aragorn221
  2. From: llong@nwu.edu (Lorraine E. Long)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help for a float problem
  5. Date: Sun, 18 Feb 96 03:20:40 GMT
  6. Organization: Northwestern University, Evanston, IL.   USA
  7. Message-ID: <4g65u8$9dg_001@nuts.nwu.edu>
  8. References: <AMCHAVAN.96Jan25112007@ac2.hq.eso.org> <4f6tt6$rt1@news.cencom.net> <311A83D1.57A3@feith.com> <4fe4m4$f1c@news.ee.net> <4fgbss$nqm@ixnews6.ix.netcom.com> <9FEB199622285820@erich.triumf.ca>
  9. NNTP-Posting-Host: aragorn221.nuts.nwu.edu
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <9FEB199622285820@erich.triumf.ca>,
  13.    bennett@erich.triumf.ca (P.Bennett) wrote:
  14. >In article <4fgbss$nqm@ixnews6.ix.netcom.com>, wzjn@ix.netcom.com (KPN ) 
  15. writes...
  16. >>Good Afternoon,
  17. >> 
  18. >>Using æHow To Program C (second edition)Æ by Deitel & Deitel, IÆm
  19. >>trying to do on page 92, example 3.17. IÆm not sure, but believe that
  20. >>my problem is where to use a æfloatÆ. 
  21. >> 
  22. >>I want to ask the user to enter gallons used, which of course, could be
  23. >>12.8. However, with the same line, I want the user to enter æ-1Æ if
  24. >>they choose to end the program.
  25. >> 
  26. >>The way it is written so far, the user can successfully exit the
  27. >>program with a -1. The user can also successfully enter whole numbers
  28. >>and get an answer. My problem arises when I enter (again) 12.8 ... the
  29. >>program goes in to an infinate loop! eh?
  30. >> 
  31. >>Conversely, if I define gallons as a float, the program does not
  32. >>recognize the æ-1Æ as an end of program. eh? 
  33. >> 
  34. >
  35. >If you expect the user to enter a decimal value like 12.8 for gallons (or
  36. >miles), you must declare gallons as float.
  37. >
  38. >However, when the user enters -1 to quit, you will be caught by the fact that
  39. >floating point numbers are never exact (Although I would expect -1 to be), so
  40. >it is unwise to compare two floats for equality.
  41. >
  42. >You could compare their difference to some small number, or, for this 
  43. problem,
  44. >simply consider any negative number as the signal to quit.
  45. >
  46. >
  47. >Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  48. >Internet: bennett@triumf.ca         | of one another only when one can be
  49. >Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  50. >TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  51. >GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  52. >
  53. >
  54. >
  55. >
  56. >
  57. >
  58. >
  59. >
  60. >
  61.  
  62.  
  63. Lorraine E. Long
  64. Northwestern University, Evanston, IL.   USA
  65. llong@nwu.edu
  66.